package com.itcast.gameforum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itcast.gameforum.common.dto.addZoneDto;
import com.itcast.gameforum.common.lang.Result;
import com.itcast.gameforum.entity.Gamezone;
import com.itcast.gameforum.entity.User;
import com.itcast.gameforum.service.GamezoneService;
import com.itcast.gameforum.service.UserService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 647zyzlq
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/gamezone/")
public class GamezoneController {

    @Autowired
    GamezoneService gamezoneService;

    @Autowired
    UserService userService;

    //得到所有游戏专区
    @GetMapping("list")
    public Result list()
    {
        List<Gamezone> list = gamezoneService.list();
        return new Result().succ(list);
    }

    //由id得到游戏专区
    @GetMapping("{id}")
    public Result getOne(@PathVariable String id)
    {
        Gamezone zone = gamezoneService.getOne(new QueryWrapper<Gamezone>().eq("id", id));
        Assert.notNull(zone,"游戏专区不存在");
        return new Result().succ(zone);
    }

    //添加游戏专区

    @PostMapping("addZone")
    public Result addZone(@Valid @RequestBody addZoneDto addZoneDto)
    {
        User user = userService.getOne(new QueryWrapper<User>().eq("username", addZoneDto.getUsername()));
        Assert.notNull(user,"用户不能为空");
        Gamezone gamezone = new Gamezone();
        gamezone.setName(addZoneDto.getName());
        gamezone.setUserid(user.getId());
        gamezoneService.save(gamezone);
        return new Result().succ("添加游戏专区成功");
    }
}
